#!/usr/bin/env python
# encoding=utf-8

from uuid import uuid4

from flask import Flask, session
from flask.ext.bootstrap import Bootstrap
from flask.ext.login import LoginManager

bootstrap = Bootstrap()

login_manager = LoginManager()
login_manager.session_protection = 'basic'
login_manager.login_view = 'auth.login'


def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'x-powerd-by: socialcredits'

    bootstrap.init_app(app)
    login_manager.init_app(app)

    from .web import web as web_blueprint
    app.register_blueprint(web_blueprint)

    from .api import api as api_blueprint
    app.register_blueprint(api_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    from .wechat import wechat as wechat_blueprint
    app.register_blueprint(wechat_blueprint)

    def add_uuid():
        if 'uuid' not in session:
            session['uuid'] = uuid4().hex
    app.before_request(add_uuid)

    app.permanent_session_lifetime = 86400 * 365 * 50

    return app
